<?php
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $model_list app\models\ArticleCategory[]
 */

$this->title = '资讯分类';
$this->params['breadcrumbs'][] = '资讯管理';
$this->params['breadcrumbs'][] = $this->title;

app\assets\TableAsset::register($this);
app\assets\AjaxAsset::register($this);
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
    <div class="form-group">
        <label for="search_name" class="sr-only">Name</label>
        <input type="text" class="form-control" name="search_name" id="search_name" placeholder="名称" value="<?php echo Yii::$app->request->get('search_name');?>" />
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary btn-sm">搜索</button>
    </div>
    <br />
    <div class="form-group">
        <a href="<?php echo Url::to(['/admin/article/edit-category']);?>" class="btn btn-success btn-sm">添加</a>
    </div>
<?php echo Html::endForm();?>
<table id="simple-table" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th class="center">
                <label class="pos-rel">
                    <input type="checkbox" class="ace" />
                    <span class="lbl"></span>
                </label>
            </th>
            <th>名称</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($model_list as $model) {
            showModel($model);
        }?>
    </tbody>
</table>
<?php
/**
 * 递归显示Model
 * @param app\models\ArticleCategory $model
 * @param string $prepend='' 名称前的附加内容（用来显示上下级）
 */
function showModel($model, $prepend = '') {?>
    <tr id="data_<?php echo $model->id;?>">
        <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $model->id;?>" /><span class="lbl"><?php echo $model->id;?></span></label></td>
        <td><?php echo $prepend, Html::encode($model->name);?></td>
        <td><?php echo app\widgets\AdminTableOp::widget(['items'=>[
                ['rbac'=>'article/category', 'icon'=>'fa fa-pencil', 'btn_class'=>'btn btn-xs btn-success', 'color'=>'green', 'tip'=>'修改', 'onclick'=>'', 'href'=>Url::to(['/admin/article/edit-category', 'id'=>$model->id])],
                ['rbac'=>'article/category', 'icon'=>'fa fa-trash', 'btn_class'=>'btn btn-xs btn-danger', 'color'=>'red', 'tip'=>'删除', 'onclick'=>'deleteArticleCategory(' . $model->id . ')', 'href'=>''],
                ['rbac'=>'article/edit', 'icon'=>'fa fa-plus', 'btn_class'=>'btn btn-xs btn-success', 'color'=>'green', 'tip'=>'添加资讯', 'onclick'=>'', 'href'=>Url::to(['/admin/article/edit', 'cid'=>$model->id])],
                ['rbac'=>'article/list', 'icon'=>'fa fa-list', 'btn_class'=>'btn btn-xs', 'tip'=>'资讯列表', 'href'=>Url::to(['/admin/article/list', 'search_cid'=>$model->id])],
            ]]);?></td>
    </tr>
    <?php if (!empty($model->subModels)) {
        foreach ($model->subModels as $sub_model) {
            showModel($sub_model, $prepend . '　　');
        }
    }?>
<?php }?>
<script>
/**
 * 删除资讯分类
 */
function deleteArticleCategory(id) {
    if (!confirm('确定要删除吗？')) {
        return false;
    }
    $.getJSON('<?php echo Url::to(['/admin/article/delete-category']);?>', {'id':id}, function(json) {
        if (callback(json)) {
            $('#data_' + id).remove();
        }
    });
}
</script>
